﻿using Microsoft.Xna.Framework;
using Terraria;
using Terraria.Audio;
using Terraria.ID;
using Terraria.ModLoader;

namespace HardcoreGame.Content.Projectiles.Melee
{
    public class FireEnchantedBeam : ModProjectile
    {
        public override void SetDefaults()
        {
            Projectile.width = 16;
            Projectile.height = 16;
            Projectile.aiStyle = 27;
            Projectile.DamageType = DamageClass.Melee;
            Projectile.penetrate = 1;
            Projectile.light = 0.2f;
            Projectile.alpha = 255;
            Projectile.friendly = true;
        }
        public override Color? GetAlpha(Color lightColor)
        {
            if (Projectile.localAI[1] >= 15f)
            {
                return new Color(255, 255, 255, Projectile.alpha);
            }
            if (Projectile.localAI[1] < 5f)
            {
                return Color.Transparent;
            }
            int num6 = (int)((Projectile.localAI[1] - 5f) / 10f * 255f);
            return new Color(num6, num6, num6, num6);
        }
        public override void AI()
        {
            if (Projectile.localAI[1] > 7f)
            {
                int num224 = Main.rand.Next(3);
                int num225 = Dust.NewDust(new Vector2(Projectile.position.X - Projectile.velocity.X * 4f + 2f, Projectile.position.Y + 2f - Projectile.velocity.Y * 4f), 8, 8, num224 switch
                {
                    0 => DustID.Torch,
                    1 => DustID.Torch,
                    _ => DustID.Torch,
                }, 0f, 0f, 100, default, 1.25f);
                Dust dust2 = Main.dust[num225];
                dust2.velocity *= 0.1f;
            }
        }
        public override void OnKill(int timeLeft)
        {
            SoundEngine.PlaySound(SoundID.Item10, Projectile.position);
            for (int num979 = 4; num979 < 24; num979++)
            {
                float num980 = Projectile.oldVelocity.X * (30f / num979);
                float num981 = Projectile.oldVelocity.Y * (30f / num979);
                int num982 = Main.rand.Next(3);
                int num983 = Dust.NewDust(new Vector2(Projectile.position.X - num980, Projectile.position.Y - num981), 8, 8, num982 switch
                {
                    0 => DustID.Torch,
                    1 => DustID.Torch,
                    _ => DustID.Torch,
                }, Projectile.oldVelocity.X * 0.2f, Projectile.oldVelocity.Y * 0.2f, 100, default, 1.8f);
                Dust dust2 = Main.dust[num983];
                dust2.velocity *= 1.5f;
                Main.dust[num983].noGravity = true;
            }
        }
        public override void OnHitNPC(NPC target, NPC.HitInfo hit, int damageDone)
        {
            if (Main.rand.NextBool(3))
            {
                target.AddBuff(BuffID.OnFire, 180);
            }
            if (Main.rand.NextBool(5))
            {
                target.AddBuff(BuffID.Burning, 120);
            }
        }
        public override void OnHitPlayer(Player target, Player.HurtInfo info)
        {
            if (Main.rand.NextBool(3))
            {
                target.AddBuff(BuffID.OnFire, 180);
            }
            if (Main.rand.NextBool(5))
            {
                target.AddBuff(BuffID.Burning, 120);
            }
        }
    }
}
